package com.cloudtasks;

import com.cloudtasks.R;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

/**
 * @author Administrator
 *
 */
public class AddItemByBrowsingPage2Activity extends Activity {
	
	String UserName = "";
	long UserID;
	String ListName = "";
	long ListID;
	String TypeName = "";
	final Context myContext = this;
	private Cursor DBcursor;
	final static String TYPE_PAGE = "type";
	private Database mDb;
	
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tab_list_typepage);
        
        try{   
	        
	        // Unwrap parameters from last activity: the User's name
	        // and also his User ID
        	Bundle param = getIntent().getExtras(); 
	        if(param != null){
	        	UserName = param.getString("uname");
		        //MyTools.displayToastMessage(myContext, UserName);
		        UserID = param.getLong("uid");
		        ListName = param.getString("");
		    	ListID = param.getLong("lid");
		    	TypeName = param.getString("tname");
	        }
        	
	        // initialize the Database for this context
	        mDb = new Database(this);
	        mDb.open();
	        ListView lv = (ListView) findViewById(R.id.types_listview);
	        DBcursor = mDb.fetchAllItemsByType(TypeName);
	        //String[] categories = getResources().getStringArray(R.array.items_array);
	        SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this,  
	        		android.R.layout.simple_list_item_1,
	        	    DBcursor,
	        	    new String[] {Database.KEY_GROC_NAME},
	        	    new int[] {android.R.id.text1}
	        	);
	        lv.setAdapter(cursorAdapter);
	        startManagingCursor(DBcursor);
	        
	        // what happens when you click one entry in the ListView
	        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {  
				@Override
				public void onItemClick(AdapterView<?> av, View view,
						int pos, long id) {
	        		String name = ((TextView) view).getText().toString();
	        		long success = mDb.createItem(name, ListID, false, TypeName);
	        		if (success == -1)
 						MyTools.displayToastMessage(myContext,
 								"Error: could not add");
 					else
 						MyTools.displayToastMessage(myContext,
 								"Successfully Added " + name);
				}  
	        });  
        }
        catch(Exception e){
        	MyTools.displayToastMessage(myContext, e.toString());
        }
    }
    public void onDestroy() {
        super.onDestroy();
     
        mDb.close();
    }
}
